package com.sftpserver;

import com.check.Check_Info;
import com.dto.config.IniDTO;
import com.utils.CheckPortUtil;
import com.utils.KillServer;
import dorkbox.notify.Notify;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import static com.fx.notify.NotifyTest.doNotify;


@Slf4j(topic = "端口检查类")
public class TestService {

    @SneakyThrows
    public TestService() {
        /*检查配置文件*/
        new Check_Info();
        String toCheckPort  = IniDTO.getPort();
        if ("".equals(toCheckPort)) {
            log.warn("--------- 端口为空 ----------");
        } else {
            //检查端口占用
            doTestService(Integer.parseInt(toCheckPort));
            log.info("--------- 端口检测通过 ----------");
        }
    }

    @SneakyThrows
    public TestService(String toCheckPort) {

        if ("".equals(toCheckPort)) {
            log.warn("--------- 端口为空 ----------");
        } else {
            //检查端口占用
            doTestService(Integer.parseInt(toCheckPort));
            log.info("--------- 端口检测通过 ----------");
        }

    }


    public void doTestService(int port){
        try {
            CheckPortUtil.checkPort(port);
            KillServer.killPort(port);
        } catch (InterruptedException e) {
            doNotify("警告","端口读取失败", Notify.DIALOG_WARNING);
        }
    }




}
